package com.qfjy.homework.yx;

public class Subject {

    //步骤一
    public interface Image {
        void display();
    }

    //步骤二
    public class RealImage implements Image {

        private String fileName;

        public RealImage(String fileName){
            this.fileName = fileName;
            loadFromDisk(fileName);
        }

        @Override
        public void display() {
            System.out.println("Displaying " + fileName);
        }

        private void loadFromDisk(String fileName){
            System.out.println("Loading " + fileName);
        }
    }

    public class ProxyImage implements Image{

        private RealImage realImage;
        private String fileName;

        public ProxyImage(String fileName){
            this.fileName = fileName;
        }

        @Override
        public void display() {
            if(realImage == null){
                realImage = new RealImage(fileName);
            }
            realImage.display();
        }
    }

    //步骤三

    public class ProxyPatternDemo {

        public  void main(String[] args) {
            Image image = new ProxyImage("test_10mb.jpg");

            // 图像将从磁盘加载
            image.display();
            System.out.println("");
            // 图像不需要从磁盘加载
            image.display();
        }
    }

    //步骤 4
    //执行程序，输出结果：

   // Loading test_10mb.jpg
   // Displaying test_10mb.jpg

    //Displaying test_10mb.jpg
}
